<template>
	<view>
		<u-navbar :is-back="false" title="　" :border-bottom="false">
			<view class="u-flex u-row-right" style="width: 100%;">
				<view class="camera u-flex u-row-center">
					<u-icon name="setting-fill" color="#000000" size="48" @click="handleToEdit"></u-icon>
				</view>
			</view>
		</u-navbar>
		<view class="u-flex user-box u-p-l-30 u-p-r-20 u-p-b-30">
			<view class="u-m-r-30">
				<u-avatar :src="pic" :text="name" bg-color="#92c8ff" :show-level="name ? true : false" size="140"></u-avatar>
			</view>
			<view class="u-flex-1">
				<view v-if="!token" class="u-font-18 u-p-b-20" @click="handleToLogin">点击登录/注册</view>
				<view v-else class="u-font-18 u-p-b-20">
					{{name}}
				</view>
				<view class="u-font-14 u-tips-color">读过{{historyReadList.length}}本</view>
			</view>
			<view class="u-m-l-10 u-p-10">
				<u-icon name="arrow-right" color="#969799" size="28"></u-icon>
			</view>
		</view>

		<view class="u-m-t-20">
			<u-cell-group>
				<u-cell-item icon="grid" title="我的书架" @click="handleToShelf"></u-cell-item>
				<u-cell-item icon="clock" title="阅读历史" @click="handleToHistory"></u-cell-item>
				<u-cell-item icon="setting" title="阅读设置" @click="handleToReadSetting"></u-cell-item>
			</u-cell-group>
		</view>

		<view class="u-m-t-20">
			<u-cell-group>
				<u-cell-item icon="trash" title="清除缓存" @click="handleToClearStorage"></u-cell-item>
				<u-cell-item icon="error-circle" title="关于我们" @click="aboutUS"></u-cell-item>
				<u-cell-item icon="bookmark" title="版本更新" :arrow="false" @click="handleToUpdate">
					<text slot="right-icon" :style="isUpdate?'color:rgb(146, 200, 255)':''">{{ isUpdate ? '监测到新版本' : '已是最新版本' }}
					</text>
				</u-cell-item>
			</u-cell-group>
		</view>
		<u-modal v-model="clearShow" :content="content" @confirm="confirmClearStorage" show-cancel-button></u-modal>
		<u-modal v-model="updateShow" :title="updateInfo.update_title" :content="updateInfo.update_content"
		 :show-cancel-button="true" @confirm="comfirmToUpdate"></u-modal>

		<u-toast ref="uToast" />

		<!-- 底部导航 -->
		<tab-bar></tab-bar>
	</view>
</template>

<script>
	import {
		verionUpdate,
		getDownloadLink
	} from '@/api/common'
	import {
		mapState
	} from 'vuex'
	export default {
		data() {
			return {
				pic: require('../../static/image/logo.png'),
				show: true,
				content: '确认要清空本地缓存?',
				clearShow: false,
				readTime: 0,
				isUpdate: false,
				version: '1.0.0',
				updateShow: false,
				updateInfo: {
					update_title: '',
					update_content: '',
					update_url: ''
				},
				idList: [],
				downloadUrl: 'https://www.sanye666.top'
			}
		},
		computed: {
			...mapState({
				token: state => state.global.token,
				name: state => state.global.name,
				historyReadList: state => state.read.historyReadList
			})
		},
		onLoad() {
			// #ifdef APP-PLUS
			this.getUpdateInfo();
			// #endif
		},
		onShow() {
			// #ifdef APP-PLUS
			this.getUpdateInfo();
			// #endif
		},
		onPullDownRefresh() {
			setTimeout(() => {
				this.$refs.uToast.show({
					title: '刷新成功',
					type: 'success',
					position: 'top'
				});
				uni.stopPullDownRefresh()
			}, 1000);
		},
		methods: {
			handleToShelf() {
				uni.switchTab({
					url: '/pages/shelf/index'
				})
			},
			handleToLogin() {
				uni.navigateTo({
					url: '/pages/center/login',
					animationType: 'slide-in-bottom',
					animationDuration: 200
				})
			},
			handleToHistory() {
				uni.navigateTo({
					url: '/pages/history/index'
				})
			},
			handleToReadSetting() {
				uni.navigateTo({
					url: '/pages/center/setting'
				})
			},
			aboutUS() {
				uni.navigateTo({
					url: `/pages/center/about?version=${this.version}`
				})
			},
			handleToEdit() {
				if (this.token) {
					uni.navigateTo({
						url: '/pages/center/edit'
					})
				} else {
					this.$refs.uToast.show({
						title: '请先登录',
						type: 'warning'
					})
				}
			},
			handleToClearStorage() {
				this.clearShow = true;
			},
			confirmClearStorage() {
				uni.clearStorage();
				this.$refs.uToast.show({
					title: '清除成功！',
					type: 'success'
				})
			},
			getUpdateInfo() {
				plus.runtime.getProperty(plus.runtime.appid, (wgtinfo) => {
					this.version = wgtinfo.version
					let header = {
						version: this.version,
						osType: 1
					}
					verionUpdate(header).then(res => {
						if (res.data.updateInfo.is_update == 1) {
							this.isUpdate = true;
							this.updateInfo = res.data.updateInfo;
						} else {
							this.isUpdate = false;
						}
					})
				});
			},
			handleToUpdate() {
				if (this.isUpdate) {
					this.updateShow = true;
				}
			},
			comfirmToUpdate() {
				// #ifdef APP-PLUS  
				this.$refs.uToast.show({
					title: '新版本开始下载',
					type: 'success',
					position: 'top'
				})
				const _self = this;
				var dtask = plus.downloader.createDownload(this.updateInfo.update_url, {}, function(d, status) {
					if (status == 200) {
						plus.runtime.install(plus.io.convertLocalFileSystemURL(d.filename), {}, {}, function(error) {
							_self.$refs.uToast.show({
								title: '安装失败',
								type: 'error',
								position: 'top'
							})
						})
					} else {
						_self.$refs.uToast.show({
							title: '更新失败',
							type: 'error',
							position: 'top'
						})
					}
				});
				dtask.start();
				// #endif
			},

			// 请求下载地址
			getDownloadLink() {
				return new Promise((resolve, reject) => {
					getDownloadLink().then(res => {
						if (res.code == 200) {
							this.downloadUrl = res.data.download_url.android;
							resolve('ok')
						} else {
							reject('res.msg')
						}
					})
				})
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #F5F8FC;
	}

	.camera {
		width: 54px;
		height: 44px;

		&:active {
			background-color: #ededed;
		}
	}

	.user-box {
		background-color: #fff;
	}

	.hideCanvasView {
		position: relative;
	}

	.hideCanvas {
		position: fixed;
		top: -99999upx;
		left: -99999upx;
		z-index: -99999;
	}

	.flex_row_c_c {
		display: flex;
		flex-direction: row;
		justify-content: center;
		align-items: center;
	}

	.modalView {
		width: 100%;
		height: 100%;
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		opacity: 0;
		outline: 0;
		transform: scale(1.2);
		perspective: 2500upx;
		background: rgba(0, 0, 0, 0.6);
		transition: all .3s ease-in-out;
		pointer-events: none;
		backface-visibility: hidden;
		z-index: 999;
	}

	.modalView.show {
		opacity: 1;
		transform: scale(1);
		pointer-events: auto;
	}

	.flex_column {
		display: flex;
		flex-direction: column;
	}

	.backgroundColor-white {
		background-color: white;
	}

	.border_radius_10px {
		border-radius: 10px;
	}

	.padding1vh {
		padding: 1vh;
	}

	.posterImage {
		width: 60vw;
	}

	.flex_row {
		display: flex;
		flex-direction: row;
	}

	.marginTop2vh {
		margin-top: 2vh;
	}
</style>
